//
//  TopicViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TopicViewController.h"

@interface TopicViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;
@property (nonatomic,strong)UISegmentedControl *segmentalControl;
@property (nonatomic,strong)NSMutableArray *hotDataSource;
@property (nonatomic,strong)NSMutableArray *newestDataSource;
@property (nonatomic,assign)NSInteger newsStart;
@property (nonatomic,assign)NSInteger newsLimit;
@end

@implementation TopicViewController

-(UISegmentedControl *)segmentalControl{
    if (!_segmentalControl) {
        _segmentalControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 20, 100, 30)];
        [_segmentalControl insertSegmentWithTitle:@"HOT" atIndex:0 animated:YES];
        [_segmentalControl insertSegmentWithTitle:@"NEW" atIndex:1 animated:YES];
    }
    return _segmentalControl;
}
-(NSMutableArray *)hotDataSource{
    if (!_hotDataSource) {
        _hotDataSource = [NSMutableArray array];
    }
    return _hotDataSource;
}

-(NSMutableArray *)newestDataSource{
    if (!_newestDataSource) {
        _newestDataSource = [NSMutableArray array];
    }
    return _newestDataSource;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    //设置布局
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.titleView = self.segmentalControl;
    self.segmentalControl.selectedSegmentIndex = 0;
    [self.segmentalControl addTarget:self action:@selector(segmentalControllAction:) forControlEvents:UIControlEventValueChanged];
    [self.hotTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"topic_cell"];
    [self.newestTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"topic_cell"];
    
    
    [self.hotTableView registerNib:[UINib nibWithNibName:@"TopicOtherCell" bundle:nil] forCellReuseIdentifier:@"topic_otherCell"];
    [self.newestTableView registerNib:[UINib nibWithNibName:@"TopicOtherCell" bundle:nil] forCellReuseIdentifier:@"topic_otherCell"];

    
    
    
    //加载tableView的数据
    [self loadHotViewWithStart:0 limit:10];
    [self loadNewViewWithStart:0 limit:10];
    //刷新数据
    self.hotTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self.hotDataSource removeAllObjects];
        [self loadHotViewWithStart:0 limit:10];
    }];
    self.newestTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self.newestDataSource removeAllObjects];
        [self loadNewViewWithStart:0 limit:10];
    }];
    //加载数据
    self.hotTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newsStart += 10;
        [self loadHotViewWithStart:self.newsStart limit:10];
    }];
    self.newestTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newsStart += 10;
        [self loadNewViewWithStart:self.newsStart limit:10];
    }];
    
//    添加手势让控制器frame复原
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    [self.navigationController.view  addGestureRecognizer:swipe];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.hotTableView becomeFirstResponder];
    
}

-(void)swipeAction:(UISwipeGestureRecognizer *)sender{
    
    
    [UIView animateWithDuration:0.2 animations:^{
        self.navigationController.view.frame = CGRectMake(kWidth/4*3, 0, kWidth, kHeight+64);
        self.isSlider = NO;
        
    }];
    
    
}


//加载最热数据
-(void)loadHotViewWithStart:(NSInteger)start limit:(NSInteger)limit{
        [RequestManager fetchDataWithUrl:kTopicURL parameter:@{@"sort":@"hot",@"start":@(start),@"limit":@(limit)} method:@"POST" completed:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//            NSLog(@"%@",dic);
            for (NSDictionary *dict in dic[@"data"][@"list"]) {
                TopicModel *model = [[TopicModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.hotDataSource addObject:model];
            }
        } updateUI:^{
            [self.hotTableView.mj_header endRefreshing];
            [self.hotTableView.mj_footer endRefreshing];
            [self.newestTableView.mj_header endRefreshing];
            [self.newestTableView.mj_footer endRefreshing];
            [self.newestTableView reloadData];

            [self.hotTableView reloadData];
            
        }];

}
//加载新数据
-(void)loadNewViewWithStart:(NSInteger)start limit:(NSInteger)limit{
        [RequestManager fetchDataWithUrl:kTopicURL parameter:@{@"sort":@"addtime",@"start":@(start),@"limit":@(limit)} method:@"POST" completed:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//            NSLog(@"%@",dic);
            for (NSDictionary *dict in dic[@"data"][@"list"]) {
                TopicModel *model = [[TopicModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.newestDataSource addObject:model];
            }
        } updateUI:^{
            [self.hotTableView.mj_header endRefreshing];
            [self.hotTableView.mj_footer endRefreshing];

            [self.newestTableView.mj_header endRefreshing];
            [self.newestTableView.mj_footer endRefreshing];
            [self.newestTableView reloadData];
            [self.hotTableView reloadData];

        }];
        
}

//segmental事件
-(void)segmentalControllAction:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex == 1) {
        self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    }else{
        self.scrollView.contentOffset = CGPointZero;
    }
    
    
}
//滚动视图
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        if (scrollView.contentOffset.x == kWidth) {
            self.segmentalControl.selectedSegmentIndex = 1;
        }else{
            self.segmentalControl.selectedSegmentIndex = 0;
        }
    }
}
//绘制UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    TopicModel *model = [[TopicModel alloc]init];
    if (tableView == self.hotTableView) {
        model = self.hotDataSource[indexPath.row];
    }else if(tableView == self.newestTableView){
        model = self.newestDataSource[indexPath.row];
    }
    if (model.coverimg.length != 0) {
        TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topic_cell" forIndexPath:indexPath];
        [cell setCellWithModel:model];
        return cell;

    }else{
        TopicOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topic_otherCell" forIndexPath:indexPath];
        [cell setCellWithModel:model];
        return cell;
    }

    
    
    
    
    
    
}
//tableViewCell的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.hotTableView) {
        return self.hotDataSource.count;
    }else
        
        return self.newestDataSource.count;
    
    
}
//cell的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (((tableView == self.hotTableView )&& ([self.hotDataSource[indexPath.row] coverimg].length != 0 )) || ((tableView == self.newestTableView )&& ([self.newestDataSource[indexPath.row] coverimg].length != 0 )) ) {
        return 200;
        
    }else{
        return 150;
    }

    
    
    
}

//tableView点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicListViewController *topicList = [[TopicListViewController alloc]initWithNibName:@"TopicListViewController" bundle:nil];
    TopicModel *model = [[TopicModel alloc]init];
    
    if (tableView == self.hotTableView) {
        topicList.contentID = [self.hotDataSource[indexPath.row] contentid];
        model = self.hotDataSource[indexPath.row];
        
    }else{
        topicList.contentID = [self.newestDataSource[indexPath.row] contentid];
        model = self.newestDataSource[indexPath.row];

    }
    topicList.model = model;
    
    [self.navigationController pushViewController:topicList animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
